package com.activiti.controler;

import com.activiti.config.SecurityUtil;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.runtime.TaskRuntime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 完成任务执行
 */
@RestController
public class myController {

    @Autowired
    private ProcessRuntime processRuntime;
    @Autowired
    private TaskRuntime taskRuntime;
    @Autowired
    private SecurityUtil securityUtil;

    //查询任务
    @RequestMapping("/task")
    public void testHelloo(){
       // securityUtil.logInAs("erdemedeiros");//指定用户认证信息
        Page<Task> taskspage = taskRuntime.tasks(Pageable.of(0, 10));//分页查询任务列表
        if(taskspage.getTotalItems()>0){
            //有任务
            for(Task task : taskspage.getContent()){
                //便利任务列表
                System.out.println("任务"+task);
                //拾取任务列表
                taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
                //执行任务
                taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build());
            }
        }else{
            System.out.println("没有任务");
        }
        taskspage = taskRuntime.tasks(Pageable.of(0, 10));//分页查询任务列表

        if(taskspage.getTotalItems()>0){
            //有任务
            for(Task task : taskspage.getContent()){
                //便利任务列表
                System.out.println("任务1"+task);
            }
        }else{
            System.out.println("没有任务");
        }
    }


    /**
     * 发布任务
     */
    @RequestMapping("/issue")
    public void issue(){

        securityUtil.logInAs("salaboy");//spring
        ProcessInstance processInstance=processRuntime.start(ProcessPayloadBuilder.start()
                .withProcessDefinitionKey("myProcess_1").build());//启动流程实例
        System.out.println("启动"+processInstance.getId());

    }
}





















